package com.xlh.service.cs.impl;

import com.google.common.collect.Lists;
import com.xlh.dao.cs.CourseWorkMapper;
import com.xlh.dao.cs.CourseWorkMapperExt;
import com.xlh.dto.cs.CourseSystemCourseDTO;
import com.xlh.dto.cs.CourseSystemTreeDTO;
import com.xlh.enums.cs.CourseSystemTypeEnum;
import com.xlh.pojo.cs.CourseWork;
import com.xlh.pojo.cs.CourseWorkExample;
import com.xlh.service.cs.CourseWorkService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zhr 2019/10/18
 */
@Service
public class CourseWorkServiceImpl implements CourseWorkService {
    @Autowired
    private CourseWorkMapper mapper;
    @Autowired
    private CourseWorkMapperExt mapperExt;

    @Override
    public List<CourseWork> selectByCourseIds(List<Long> courseIds, Integer type) {
        CourseWorkExample example = new CourseWorkExample();
        CourseWorkExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false).andTypeEqualTo(type);
        if (CollectionUtils.isNotEmpty(courseIds)) {
            criteria.andCourseIdIn(courseIds);
        }
        List<CourseWork> list = mapper.selectByExample(example);
        return CollectionUtils.isEmpty(list) ? Lists.newArrayList() : list;
    }

    @Override
    public void edit(List<CourseSystemTreeDTO> workList) {
        List<CourseWork> list = Lists.newArrayList();

        int orderNum = 0;
        for (int i = 0; i < workList.size(); i++) {
            CourseSystemTreeDTO dto = workList.get(i);

            List<CourseSystemCourseDTO> courseList = dto.getCourseList();
            for (int j = 0; j < courseList.size(); j++) {
                CourseSystemCourseDTO courseDTO = courseList.get(j);
                list.add(new CourseWork(courseDTO.getId(), courseDTO.getType(), dto.getId(), orderNum++));
            }
        }

        coverSave(list);
    }

    @Override
    public List<CourseSystemTreeDTO> getCourseWork(List<CourseSystemCourseDTO> systemCourseList, List<CourseSystemCourseDTO> notSystemCourseList) {
        List<CourseSystemTreeDTO> list = mapperExt.select();
        return CollectionUtils.isEmpty(list) ? Lists.newArrayList() : list;
    }

    @Override
    public void deleteByCourseIds(List<Long> courseIds) {
        if (CollectionUtils.isEmpty(courseIds)) {
            return;
        }

        CourseWork deleteData = new CourseWork();
        deleteData.setDeleted(true);

        CourseWorkExample example = new CourseWorkExample();
        example.createCriteria().andCourseIdIn(courseIds).andTypeEqualTo(CourseSystemTypeEnum.NOT_SYSTEM_COURSE.getType());

        mapper.updateByExampleSelective(deleteData, example);
    }

    @Override
    public void deleteByWorkIds(List<Long> workIds) {
        if (CollectionUtils.isEmpty(workIds)) {
            return;
        }

        CourseWork deleteData = new CourseWork();
        deleteData.setDeleted(true);

        CourseWorkExample example = new CourseWorkExample();
        example.createCriteria().andWorkIdIn(workIds);

        mapper.updateByExampleSelective(deleteData, example);
    }

    /**
     * 覆盖保存,先删除原始数据,再将新数据插入
     *
     * @param list
     */
    private void coverSave(List<CourseWork> list) {
        // 删除原始数据
        CourseWork deleteData = new CourseWork();
        deleteData.setDeleted(true);

        CourseWorkExample example = new CourseWorkExample();
        example.createCriteria().andDeletedEqualTo(false);
        mapper.updateByExampleSelective(deleteData, example);

        // 插入数据
        if (CollectionUtils.isNotEmpty(list)) {
            mapperExt.insertBatch(list);
        }
    }
}
